
package guoan.web.webCustomer;

import guoan.core.dto.DataGridModel;
import guoan.core.dto.PageResponse;
import guoan.core.dto.RestResponse;
import guoan.domain.Exam;
import guoan.domain.ExamItem;
import guoan.service.ExamItemService;
import guoan.service.ExamService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;

/**
 * @ClassName: ExamItemController
 * @Description: TODO
 * @author Administrator
 * @date 2015年4月21日 上午9:25:27
 * 
 */
@Controller
@RequestMapping("/webCustomer/examItem")
public class ExamItemController {
    /**
     * @Fields log : 日志
     */
    protected final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * @Fields examItemService : 预约考试报名结果 server
     */
    @Autowired
    private ExamItemService examItemService;

    /**
     * @Fields examService : TODO
     */
    @Autowired
    private ExamService examService;

    /**
     * @Title：index
     * @Description: 考试的预约结果界面
     * @author Administrator
     * @date 2015年4月21日 上午9:23:40
     * @param examId 考试ID
     * @param model 界面model
     * @return String
     */
    @RequestMapping(value = "exam_{examId}", method = RequestMethod.GET)
    public String index(@PathVariable("examId") Long examId, Model model){
        log.info("[考试的预约结果界面] 开始,对应ID的考试信息  <- {}", examId);
        Exam exam = examService.findOne(examId);
        if (exam != null) {
            log.info("[考试的预约结果界面] 结束");
            model.addAttribute("exam_openHours",
                    Splitter.on(",").split(exam.getOpenHours()).iterator());
            model.addAttribute("exam", exam);
            return "/webCustomer/examItem/index";
        } else {
            log.info("[考试的预约结果界面] 结束  没有找到对应ID的考试信息  <- {}", examId);
            return "/webCustomer/404";
        }
    }

    /**
     * @Title：data
     * @Description: TODO
     * @author Administrator
     * @date 2015年4月21日 上午9:25:37
     * @param dataGridModel
     * @param type
     * @return PageResponse<ExamItem>
     */
    @RequestMapping(value = "data", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<ExamItem> data(DataGridModel dataGridModel,
            Long examId, Long openHours){
        return examItemService.page(examId, openHours, dataGridModel);
    }

    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    public RestResponse<Boolean> del(Long id){
        Preconditions.checkNotNull(id, "参数错误");
        examItemService.delete(id);
        return RestResponse.ok(true);
    }
}
